package org.kele.sc2020.proivder.service.impl;

import cn.hutool.core.collection.ListUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.kele.sc2020.proivder.service.GroupTaskService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class GroupTaskServiceImpl implements GroupTaskService {

    @Override
    public List<List<Integer>> groupIntoList(List<Integer> ids, int instanceCount) {
        // 如果任务组数量 >临界值(实例数量*10) 实例数量>0 才进行分组;
        if (ids.size() > groupCriticalSize(instanceCount) && instanceCount > 0) {
            int groupSize = caculateGroupSize(ids.size(), instanceCount);
            return Lists.partition(ids, groupSize);
        }

        return Collections.singletonList(ids);

    }

    @Override
    public Map<String, List<Integer>> groupIntoMap(List<Integer> ids, int instanceCount) {


        return null;
    }

    private int caculateGroupSize(int size, int instanceCount) {
        return size % instanceCount == 0 ? size / instanceCount : size / instanceCount + 1;
    }
}
